export const request = (options) => {
	return new Promise((resolve, reject) => {
		const token = uni.getStorageSync('token');

		uni.request({
			url: options.url,
			method: options.method || 'GET',
			data: options.data,
			header: {
				'Content-Type': 'application/json',
				'Authorization': token ? `Bearer ${token}` : '',
				...options.header
			},
			success: (res) => {
				if (res.statusCode === 401) {
					// Handle unauthorized
					uni.removeStorageSync('token');
					uni.removeStorageSync('user');
					uni.navigateTo({
						url: '/pages/login/login'
					});
					reject(new Error('未授权，请重新登录'));
					return;
				}

				if (res.statusCode >= 200 && res.statusCode < 300) {
					resolve(res.data);
				} else {
					reject(new Error(res.data.message || '请求失败'));
				}
			},
			fail: (err) => {
				reject(err);
			}
		});
	});
};